package com.mew.chess.core.common.handler;

import com.mew.chess.core.common.handler.*;
import com.mew.common.config.IPSourceProperties;
import com.mew.common.config.SpringContextBean;
import com.zebra.common.TmsServerType;
import com.zebra.remoting.IpInfoProperties;
import com.zebra.remoting.config.TmsServerInfo;
import com.zebra.remoting.tms.TmsServerApplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(1)
@Slf4j
public class StartInitialize implements ApplicationRunner {


    @Value("${game.contains}")
    private int contains;
    @Value("${game.list}")
    private String gameList;

    @Autowired
    private ChessServerStartService chessServerStartService;
    @Autowired
    private TmsServerApplication tmsServerApplication;
    @Autowired
    private IPSourceProperties ipSourceProperties;

    public void start() throws Exception {
        InitializeStartMapping.initializeMapping();
        InitializeEEHandlerMapping.initializeMapping();
        InitializePlayHandlerMapping.initializeMapping();
        TmsServerInfo serverInfo = chessServerStartService.startBefore(gameList, contains, TmsServerType.Chess);
        IpInfoProperties properties = IpInfoProperties.builder()
                .ipOut(ipSourceProperties.getIpOut())
                .ipIn(ipSourceProperties.getIpIn())
                .build();
        tmsServerApplication.run(properties, ChessHandlerMapCmdType.getMap(), SpringContextBean.getBean(ChannelHandlerService.class), new ChessOnlineNumberService(), serverInfo);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        start();
    }

}
